<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/8
 * Time: 13:35
 */

namespace app\admin\controller;

use app\admin\model\CashModel;
class Cash extends Base{
    public function index(){
        if(request()->isAjax()){
            $param = input('param.');
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;
            $where = [];
            if (paramValid($param,'mobile')) $where['mobile'] = ['like', '%' . $param['mobile'] . '%'];
            if (paramValid($param,'realname')) $where['realname'] = ['like', '%' . $param['realname'] . '%'];
            //状态：1申请中,2提现成功,3审核不通过,4提现失败
            if(isset($param['type']) && $param['type']!=0 && $param['type']<5){
                $where['status']=$param['type'];
            }
            $model = new CashModel();
            $selectResult = $model->getOrder($where, $offset, $limit);
            // 拼装参数
            foreach($selectResult as $key=>$vo){
                $selectResult[$key]['addtime']=format_date($vo['addtime']);
                $selectResult[$key]['check_time']=format_date($vo['check_time']);
                $selectResult[$key]['status']=cashType($vo['status']);
                $selectResult[$key]['operate'] = showOperate($this->makeButton($vo['cash_id']));
            }

            $return['total'] = $model->getAll($where);  //总数据
            $return['rows'] = $selectResult;

            return json($return);
        }
        $param=input('param.');
        $data=array('user_id'=>0);
        if(isset($param['user_id'])){
            $data['user_id']=$param['user_id'];
        }
        $data['sch']=json_encode(array(
            array('id'=>'mobile','name'=>'手机号'),
            array('id'=>'realname','name'=>'真实姓名')
        ));
        $this->assign([
            'data' => $data
        ]);
        return $this->fetch();
    }
    //审核
    public function pass(){
        $param=input('param.');
        $model = new CashModel();
        $result = $model->pass($param['id']);
        return json($result);
    }
    //驳回
    public function reject(){
        $param=input('param.');
        $model = new CashModel();
        $result = $model->reject($param['id']);
        return json($result);
    }
    //删除
    public function del(){
        $param=input('param.');
        $model = new CashModel();
        $result = $model->del($param['id']);
        return json($result);
    }
    private function makeButton($id){
        //状态：1申请中,2提现成功,3审核不通过,4删除作废,5提现失败'
        return [
            '通过' => [
                'auth' => 'cash/pass',
                'href' => "javascript:cashPass('" .$id ."')",
                'btnStyle' => 'primary',
                'icon' => 'fa fa-tasks'
            ],
            '驳回' => [
                'auth' => 'cash/reject',
                'href' => "javascript:cashReject('" .$id ."')",
                'btnStyle' => 'info',
                'icon' => 'fa fa-retweet'
            ],
            /*
            '删除' => [
                'auth' => 'cash/del',
                'href' => "javascript:cashDel(" .$id .")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-trash-o'
            ]
            */
        ];
    }
}